package cn.easybuy.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.ProductCategoryDao;
import cn.easybuy.entity.ProductCategory;
/**
 * 商品分类
 * @author Tom
 *
 */
public class ProductCategoryDaoImpl extends BaseDao implements ProductCategoryDao {

	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs= null;
	
	@Override
	public List<ProductCategory> getAllProductCategroy() {
		// TODO Auto-generated method stub
		String sql ="select * from  easybuy_product_category order by parentId ";
		
		List<ProductCategory> list = new ArrayList<ProductCategory>(); 
		
		try {
			conn=super.getConnection();
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			ProductCategory pc =null;
			while(rs.next()) {
				/*pc = new ProductCategory();
				pc.setId(rs.getInt("id"));
				pc.setName(rs.getString("name"));
				pc.setParentId(rs.getInt("parentId"));
				pc.setType(rs.getInt("type"));
				pc.setIconClass(rs.getString("iconclass"));
				*/
				pc =tableToClass(rs);
				
				list.add(pc);
			}
			
			return list;
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			close(conn, pstmt, rs);
		}
		
		return null;
	}

	@Override
	public ProductCategory tableToClass(ResultSet rs) throws Exception {
		// TODO Auto-generated method stub
		ProductCategory pc = new ProductCategory();
		pc.setId(rs.getInt("id"));
		pc.setName(rs.getString("name"));
		pc.setParentId(rs.getInt("parentId"));
		pc.setType(rs.getInt("type"));
		pc.setIconClass(rs.getString("iconclass"));
		return pc;
	}

}
